/**
 * Created by zl on 2017/4/13.
 */
angular.module('home.controller', [])
    .controller('homeController', ['$scope','$homeService', '$userService','$sce', function ($scope, $homeService, $userService, $sce) {
        window.LOCAL_STORE.context.open(GLOBAL_SESSION);
        $scope.user  = window.LOCAL_STORE.context.session.user || {};

        $homeService.getHomePageData().then(function (results) {
            if(results && results.code == 0){
                results.data = results.data || {};
                $scope.charityReports = results.data.charityReports || [];
                $scope.charityImageReports = results.data.charityImageReports || [];
                $scope.donateActivities = results.data.donateActivities || [];
                $scope.policyRules = results.data.policyRules || [];
                $scope.otherInfo = results.data.otherInfo || [];
                $scope.recommendVideos = results.data.recommendVideos || [];
            }
            else{
                console.error('查询数据失败');
            }

            //轮播图
            setTimeout(function () {
                $('#slider').nivoSlider();
            }, 200);
        });

        $scope.trustSrc = function (modle) {
            if(modle && modle.videoUrl){
                return $sce.trustAsResourceUrl(modle.videoUrl);
            }
            else{
                return "";
            }
        }


        $scope.login = function () {
            $scope.checkLoginInfo();
            if($scope.checkResult.isValid){
                var params = {password:$scope.user.password};
                if(verifyPhoneNum($scope.user.emailOrPhone))
                {
                    params.phoneNumber = $scope.user.emailOrPhone;
                }
                else if(verifyEmail($scope.user.emailOrPhone)){
                    params.email = $scope.user.emailOrPhone;
                }
                $userService.login(params,function(data){
                    if(data && data.data){
                        var resultData = data.data;
                        if(resultData.result){
                            //登录成功
                            window.LOCAL_STORE.context.open(GLOBAL_SESSION);
                            window.LOCAL_STORE.context.session.user = resultData.user;
                            window.LOCAL_STORE.context.rme(GLOBAL_SESSION);
                            $scope.user = resultData.user;
                        }
                        else {
                            $scope.checkResult = {
                                isValid:false,
                                validMessage : resultData.message
                            }
                            alert('登录失败:'+ resultData.message);

                        }

                    }else{
                        $scope.checkResult = {
                            isValid:false,
                            validMessage : '请求失败,网络异常'
                        }
                    }

                },function(err){
                    console.log(JSON.stringify(err));
                    $scope.checkResult = {
                        isValid:false,
                        validMessage : '网络异常'
                    }
                });

            }
        }

        $scope.checkLoginInfo = function () {
            $scope.checkResult = {
                isValid:false,
                validMessage : ''
            }

            var isValid = verifyPhoneNum($scope.user.emailOrPhone) || verifyEmail($scope.user.emailOrPhone);
            if(!isValid){
                $scope.checkResult.isValid = isValid;
                $scope.checkResult.validMessage="登录用户名不合法";

                return ;
            }

            if(!$scope.user.password){
                $scope.checkResult.isValid = false;
                $scope.checkResult.validMessage="请输入密码";
                return ;
            }

            $scope.checkResult.isValid = true;

        };

        $scope.logout = function () {
            window.LOCAL_STORE.context.session.user = {};
            window.LOCAL_STORE.context.rme(GLOBAL_SESSION);
            $scope.user  = {};
        }

    }])
    .factory('$homeService', ['$http', function ($http) {
        return {
            getHomePageData: function () {
                return $http({
                    method: 'GET',
                    url: '/homePageInfo'
                }).then(function (result) {
                    return result.data;
                });
            }
        }

    }]);